package gui;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.util.Observable;

import javax.swing.AbstractAction;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import conference.Manuscript;
import conference.Review;
import conference.Status;

public class ManuscriptDecisionPanel extends Observable {
	private static final int ROWS = 30;
	private static final int COLUMNS = 50;
	private static final String TITLE = "Evaluate a Manuscript: ";
	private JPanel myPanel;
	private Manuscript myManuscript;
	private Status myStatus;
	
	public ManuscriptDecisionPanel(Manuscript theManuscript) {
		myManuscript = theManuscript;
		myStatus = myManuscript.getStatus();
		myPanel = new JPanel(new BorderLayout());
		initPanel();
	}
	
	@SuppressWarnings("serial")
	private void initPanel() {
		JPanel buttonPanel = new JPanel();
		JPanel northPanel = new JPanel();
		northPanel.setLayout(new BoxLayout(
				northPanel, BoxLayout.Y_AXIS));
		JPanel centerPanel = 
			new JPanel(new GridLayout(1,2));
		JTextArea manuscriptText = 
			new JTextArea(myManuscript.getFileContents(),
					ROWS, COLUMNS);
		manuscriptText.setLineWrap(Boolean.TRUE);
		manuscriptText.setEditable(Boolean.FALSE);
		JTextArea reviewText = new JTextArea(getReviews(),
				ROWS, COLUMNS);
		reviewText.setLineWrap(Boolean.TRUE);
		reviewText.setEditable(Boolean.FALSE);
		JScrollPane manuscriptPane = 
			new JScrollPane(manuscriptText);
		JScrollPane reviewPane = 
			new JScrollPane(reviewText);
		centerPanel.add(manuscriptPane);
		centerPanel.add(reviewPane);
		
		JLabel title = new JLabel("Title: " + myManuscript.getTitle());
		JLabel author = new JLabel("Author: " + myManuscript.
					getAuthor().getLastCommaFirst());
		JLabel status = new JLabel("Status: " + myManuscript.
				getStatus().toString());
		northPanel.add(title);
		northPanel.add(author);
		northPanel.add(status);

		myPanel.add(northPanel, BorderLayout.NORTH);
		myPanel.add(centerPanel, BorderLayout.CENTER);

		buttonPanel.add(new JButton(
			new AbstractAction(Status.ACCEPTED.toString()) {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					myStatus = Status.ACCEPTED;
					JOptionPane.showMessageDialog(myPanel, 
						myManuscript.getTitle() + 
						" Status has been updated to " + 
						myStatus.toString());
					returnToViewList();
				}
			}
		));
		buttonPanel.add(new JButton(
			new AbstractAction(Status.DENIED.toString()) {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					myStatus = Status.DENIED;
					JOptionPane.showMessageDialog(myPanel, 
						myManuscript.getTitle() + 
						" Status has been updated to " + 
						myStatus.toString());
					returnToViewList();
				}
			}
		));
		buttonPanel.add(new JButton(
			new AbstractAction(Status.UNDECIDED.toString()) {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					myStatus = Status.UNDECIDED;
					JOptionPane.showMessageDialog(myPanel, 
						myManuscript.getTitle() + 
						" Status has been updated to " + 
						myStatus.toString());
					returnToViewList();
				}
			}
		));
		buttonPanel.add(new JButton(
			new AbstractAction("Cancel") {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					returnToViewList();
				}
			}
		));
		buttonPanel.add(GuiController.getSwitchConferenceButton());
		buttonPanel.add(GuiController.getSwitchUserButton());
		buttonPanel.add(GuiController.getSaveButton());
		buttonPanel.add(GuiController.getQuitButton());
		
		myPanel.add(buttonPanel, BorderLayout.SOUTH);
		
	}
	
	private String getReviews() {
		StringBuilder sb = new StringBuilder();
		for(Review review : myManuscript.getReviews()) {
			sb.append("Reviewer: ");
			sb.append(review.getReviewer().getLastCommaFirst());
			sb.append("\n\n    ");
			sb.append(review.getFileContents());
			sb.append("\n\n");
		}
		return sb.toString();
	}

	private void returnToViewList() {
		setChanged();
		notifyObservers(State.EVAL_MANUSCRIPT);
	}

	public JPanel getPanel() {
		return myPanel;
	}
	
	public String getTitle() {
		return TITLE + myManuscript.getTitle();
	}
	
	public Status getStatus() {
		return myStatus;
	}
	
	public Manuscript getManuscript() {
		return myManuscript;
	}
}
